import { createRouter, createWebHistory } from 'vue-router';
import store from '@/store';
let routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import( '@/views/Login/index.vue' )
  },
  {
    path: '/home',
    name: 'home',
    component: () => import( '@/views/Home/index.vue' )
  },
]

let router = createRouter( {
  history: createWebHistory(),
  routes
} )

router.beforeEach( ( to, from, next ) => {
  if ( to.path === '/login' ) {
    if ( store.state.token ) {
      next('/home');
    } else {
      next();
    }
  } else {
    if ( store.state.token ) {
      next();
    } else {
      next( '/login' );
    }
  }
})

export default router;